package com.cn.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.cn.model.JavaBeanPerson;


/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
		return "home";
	}
	
	/**
	 * 返回iReport报表视图
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/report", method = RequestMethod.GET)
	public ModelAndView report(ModelMap model) {
		// 报表数据源
		JRDataSource jrDataSource = new JRBeanCollectionDataSource(JavaBeanPerson.getList());
		
		model.put("format", "pdf"); // 报表格式
		model.put("IS_IGNORE_PAGINATION", false); // 不分页
		model.put("datasource", jrDataSource);
		
		ModelAndView modelAndView = new ModelAndView("MvcIReportExample", model);
		return modelAndView;
	}
	
	/**
	 * 返回iReport报表视图，支持PDF显示中文
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/reportChs", method = RequestMethod.GET)
	public ModelAndView reportChs(ModelMap model) {
		// 报表数据源
		JRDataSource jrDataSource = new JRBeanCollectionDataSource(JavaBeanPerson.getListChs());
				
		model.put("format", "pdf"); // 报表格式
		model.put("datasource", jrDataSource);
		ModelAndView modelAndView = new ModelAndView("MvcIReportChsExample", model);

		
		return modelAndView;
	}
}
